package com.aidex.web.controller.biz;

import com.aidex.biz.domain.AccountInfo;
import com.aidex.biz.domain.AccountLog;
import com.aidex.biz.domain.BillInfo;
import com.aidex.biz.domain.UserTixian;
import com.aidex.biz.service.*;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 用户提现Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-10-27
 */
@RestController
@RequestMapping("/biz/bizUserTixian")
public class UserTixianController extends BaseController {
    @Autowired
    private UserTixianService userTixianService;
    @Autowired
    private WxPayService wxPayService;
    @Autowired
    private AccountInfoService accountInfoService;
    @Autowired
    private AccountLogService accountLogService;

    @Autowired
    private BillInfoService billInfoService;

    /**
     * 查询用户提现列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserTixian:list')")
    @GetMapping("/list")
    public R<PageInfo> list(UserTixian userTixian, HttpServletRequest request, HttpServletResponse response) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            userTixian.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        userTixian.setPage(new PageDomain(request, response));
        userTixian.getPage().setIsAsc(userTixian.getOrder());
        userTixian.getPage().setOrderByColumn(userTixian.getSort());
        return R.data(userTixianService.findPage(userTixian));
    }

    /**
     * 获取用户提现详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserTixian:query')")
    @GetMapping(value = "/{id}")
    public R<UserTixian> detail(@PathVariable("id") String id) {
        return R.data(userTixianService.get(id));
    }

    /**
     * 新增用户提现
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserTixian:add')")
    @Log(title = "用户提现", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated UserTixian userTixian) {
        return R.status(userTixianService.save(userTixian));
    }

    /**
     * 修改用户提现
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserTixian:edit')")
    @Log(title = "用户提现", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated UserTixian userTixian) {
        if("2".equals(userTixian.getStatus())){
            userTixian.setPayTime(new Date());
        }
        if(userTixianService.save(userTixian)){
            if("2".equals(userTixian.getStatus())){
                userTixian=userTixianService.get(userTixian.getId());
                BillInfo billInfo=billInfoService.get(userTixian.getBillId());
                billInfo.setStatus("2");
                billInfoService.save(billInfo);
            }
        }
        return R.status(true);
    }


    /**
     * 删除用户提现
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserTixian:remove')")
    @Log(title = "用户提现", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(userTixianService.deleteUserTixianByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserTixian:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(userTixianService.findMaxSort(new UserTixian()));
    }

    /**
     * 导出用户提现列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserTixian:export')")
    @Log(title = "用户提现", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(UserTixian userTixian) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            userTixian.setCompanyId(null);
        } else {
            userTixian.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<UserTixian> list = userTixianService.findList(userTixian);
        ExcelUtil<UserTixian> util = new ExcelUtil<UserTixian>(UserTixian.class);
        return util.exportExcel(list, "用户提现数据");
    }

    /**
     * 用户提现重新打款
     */

    @Log(title = "用户提现重新打款", businessType = BusinessType.OTHER)
    @PostMapping("/rePay")
    public R rePay(@RequestBody UserTixian userTixian) {
        userTixian = userTixianService.get(userTixian.getId());
        userTixian.setStatus("0");
        return R.status(userTixianService.save(userTixian));
    }


    /**
     * 退回用户余额
     */

    @Log(title = "退回用户余额", businessType = BusinessType.OTHER)
    @PostMapping("/rollbackAccount")
    public R rollbackAccount(@RequestBody UserTixian userTixian) {
        AccountInfo accountInfo = accountInfoService.get(userTixian.getAccountId());
        accountInfo.setAccount(accountInfo.getAccount().add(userTixian.getMoney()));
        accountInfoService.save(accountInfo);
        AccountLog accountLog = new AccountLog();
        accountLog.setId(StringUtils.Getnum());
        accountLog.setIsNewRecord();
        accountLog.setUserAccountId(accountInfo.getId());
        accountLog.setMoney(userTixian.getMoney());
        accountLog.setType("提现失败-退回");
        accountLog.setStartMoney(accountInfo.getAccount().subtract(accountLog.getMoney()));
        accountLog.setEndMoney(accountLog.getStartMoney().add(accountLog.getMoney()));
        accountLog.setTargetAccountId(accountInfo.getId());
        accountLog.setCompanyId(userTixian.getCompanyId());
        accountLogService.save(accountLog);
        userTixian = userTixianService.get(userTixian.getId());
        userTixian.setStatus("-1");
        userTixianService.save(userTixian);
        return R.success();
    }


    /**
     * 对账信息
     */
    @Log(title = "对账信息", businessType = BusinessType.OTHER)
    @PostMapping("/getTransferBatchesDetails")
    public R getTransferBatchesDetails(@RequestBody UserTixian userTixian) {
        userTixian = userTixianService.get(userTixian.getId());
        return R.success(userTixianService.getTransferBatchesDetails(userTixian).getString("message"));
    }


    /**
     * 获取用户所有提现金额
     */
    @Log(title = "获取用户所有提现金额", businessType = BusinessType.OTHER)
    @GetMapping("/getUserTixianAllAmount")
    public R getUserTixianAllAmount(UserTixian userTixian) {
        return R.data(userTixianService.getUserTixianAllAmount(userTixian.getUserInfoId()));
    }

}
